/**
* 操作面板界面
 * @author none
 */
class OperatePanelView extends eui.Component {
    public qipaiButton: eui.Button;//弃牌按钮
    public genzhuButton: how.Button;//跟注按钮
    public jiazhuButton: how.Button;//加注按钮
    public quickOperatePanel: QuickOperatePanel;//快速操作面板
    public ranghuoqiButton: how.CheckBox;//让或弃单选框按钮
    public zidongrangpaiButton: how.CheckBox;//自动让牌单选框按钮
    public genrenhezhuButton: how.CheckBox;//跟任何注单选框按钮
    /** 当前选中的单选按钮 */
    public _preClick: how.CheckBox = null;
    public autoSitDown: how.Button;//自动坐下按钮
    public operateGroup: eui.Group;//操作面板
    public autoOperateGroup: eui.Group;//自动操作面板
    public buttonBar: eui.Group;//自动坐下按钮条
    public showCards: how.Button;//亮牌按钮
    public gameEndShowCard: how.CheckBox;//结束时亮牌按钮
    /** 操作风险提示提示 */
    public tipDialog: how.Dialog;
    /** 最小跟注数量 */
    public lTurnLessScore:number = 0;
    /** 最新加注数量 */
    public lAddLessScore:number = 0;
    /** 最大加注数量 */
    public lTurnMaxScore:number = 0;
    /** 是否全压了 */
    public allin:boolean = false;
    /** 底池 */
    public dichiValue:number;
    /** 自己的playview */
    public myPv:NewPlayerView;

    public constructor() {
        super();
        this.skinName = "gameScene.BackMenuPanel";
    }
    protected childrenCreated(): void {
        this.qipaiButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onQipaiButton, this, true);
        this.genzhuButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenzhuButton, this);
        this.jiazhuButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onJiazhuButton, this);
        this.ranghuoqiButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCheckBoxButton, this);
        this.zidongrangpaiButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCheckBoxButton, this);
        this.genrenhezhuButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCheckBoxButton, this);
        this.showCards.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onShowCards, this);
        this.gameEndShowCard.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGameEndShowCard, this);

        this.quickOperatePanel.daMang3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ondaMang3, this);
        this.quickOperatePanel.daMang5.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ondaMang5, this);
        this.quickOperatePanel.daMang10.addEventListener(egret.TouchEvent.TOUCH_TAP, this.ondaMang10, this);
        this.quickOperatePanel.allInBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onallInBtn, this);
    }
    /** 按下弃牌按钮 */
    public onQipaiButton(e:egret.TouchEvent): void {
        if (this.myPv.addScore > 0) {
            this.tipDialog = how.Dialog.show(LanguageConfig.qipaiTip, () => {
                    this.qipai();
            }, () => {
                this.tipDialog = null;
            }, this);
        } else {
                this.qipai();
        }
        e.stopPropagation();
        //this.report("hideBackMenuGroup");
    }
    /** 确认弃牌 */
    private qipai(){
        this.showGameEndShowCard();
        let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
        pv.stopClock();
        pv.showCards();
        // NewGameSceneData.getInstance().isOperate = true;
        WebSocketInstance.getHallSocket().send(CMDConfig.SEND_QIPAi);
    }

    /*
     * 按下跟注按钮
     * */
    public onGenzhuButton(e:egret.TouchEvent): void {
        // this.report("onGenzhuButton");
        this.genzhu();
        // this.report("hideBackMenuGroup");
    }
    /*
     * 按下加注按钮
     * */
    public onJiazhuButton(e:egret.TouchEvent): void {
        // this.report("onJiazhuButton");
        if (this.quickOperatePanel.visible) {
            // NewGameSceneData.getInstance().isOperate = true;
            var lessScore: number = this.quickOperatePanel.score;
            lessScore = lessScore > this.lTurnMaxScore ? this.lTurnMaxScore : lessScore;
            this.jiazhu(lessScore);
        } else {
            // this.callUI("shwoQuickOperatePanel");
            this.shwoQuickOperatePanel();
            // this.jiazhuPanelVisible = true;
        }
        // this.report("hideBackMenuGroup");
        e.stopPropagation();
    }
    /** 显示加注面板 */
    public shwoQuickOperatePanel(): void {
        this.quickOperatePanel.setData();
        this.quickOperatePanel.visible = true;
    }
    /*
    * 隐藏加注面板
    * */
    public hideQuickOperatePanel(): void {
        this.quickOperatePanel.visible = false;
        // this.report("hideBackMenuGroup");
    }
    /*
    * 按下单选框按钮
    * */
    public onCheckBoxButton(event: egret.TouchEvent): void {
        //this.report("onCheckBoxButton", event);
        var ta: how.CheckBox = event.currentTarget;
        if (this._preClick && this._preClick == ta) {
            ta.selected = false;
            this._preClick = null;
        } else {
            if (this._preClick) {
                this._preClick.selected = false;
            }
            this._preClick = ta;
        }
        // this.report("hideBackMenuGroup");
        how.SoundManager.playEffect("window_open_mp3");
    }
    /** 设置可以操作UI */
    public setOperateUIOperatePanel(): void {
        // NewGameSceneData.getInstance().isOperateFlag = true;
        this.visible = true;
        // this.isSend = true;
        if (this.canOperate()) {
            this.setOperateUI();
        }
    }

    /** 是否可以操作 */
    public canOperate(): boolean {
        var canOperate: boolean = true;
        if (this._preClick) {
            switch (this._preClick.name) {
                case "ranghuoqiButton":
                    if (this.lTurnLessScore == 0) {//如果最小加注是0则说明可以让牌
                        this.genzhu();//发送让牌
                        canOperate = false;
                    } else {
                        this.qipai();//发送弃牌
                        canOperate = false;
                    }
                    break;
                case "zidongrangpaiButton":
                    if (this.lTurnLessScore > 0) {//如果超过自己跟注额度
                        if (this.lTurnLessScore == this.lTurnMaxScore) {
                            this.setPreOperateUI();
                            WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CALL_CHIP, { lScore: this.lTurnLessScore });
                            this.myPv.stopClock();
                            // NewGameSceneData.getInstance().myLAddScoreCount += this.lTurnLessScore;
                            canOperate = false;
                        } else {
                            this.jiazhu(this.lTurnLessScore);
                            canOperate = false;
                        }
                    } else if (this.lTurnLessScore == 0) {//如果小于自己下金额
                        this.genzhu();//发送让牌
                        canOperate = false;
                    }
                    break;
                case "genrenhezhuButton"://跟任何注
                    this.genzhu();//发送跟注
                    canOperate = false;
                    break;
            }
            this._preClick.selected = false;
            this._preClick = null;
        }
        return canOperate;
    }
    /** 发送加注 */
    public jiazhu(addScore): void {
        // if (this.isSend) {
            if (this.lTurnMaxScore == addScore) {
                this.tipDialog = how.Dialog.show(LanguageConfig.allinTip, () => {
                    // if (this.isSend) {
                        // this.jiazhuPanelVisible = false;
                        this.setPreOperateUI();
                        // this.isSend = false;
                        // NewGameSceneData.getInstance().isOperate = true;
                        WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CALL_CHIP, { lScore: addScore });
                        //停止自己出牌倒计时
                        // this.communicate(NewPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
                        //  let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
                        // pv.stopClock();
                        this.myPv.stopClock();
                        // pv.showCards();
                        // NewGameSceneData.getInstance().myLAddScoreCount += addScore;
                        // this.callUI("hideOrShowQipaiButton", false);
                    // }
                }, () => {
                    this.tipDialog = null;
                }, this);
            } else {
                // this.jiazhuPanelVisible = false;
                // this.isSend = false;
                this.setPreOperateUI();
                // NewGameSceneData.getInstance().isOperate = true;
                WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CALL_CHIP, { lScore: addScore });
                //停止自己出牌倒计时
                // this.communicate(NewPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
                //  let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
                // pv.stopClock();
                // pv.showCards();
                // NewGameSceneData.getInstance().myLAddScoreCount += addScore;
            }
        // }
    }
    /** 发送跟注 */
    public genzhu(): void {
        // if (this.isSend) {
            // this.isSend = false;
            this.setPreOperateUI();
            //停止自己出牌倒计时
            // this.communicate(NewPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
            let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
            pv.stopClock();
            // pv.showCards();
            // NewGameSceneData.getInstance().isOperate = true;
            var lessScore: number = this.lTurnLessScore ? this.lTurnLessScore : 0;
            WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CALL_CHIP, { lScore: lessScore });
            // NewGameSceneData.getInstance().myLAddScoreCount += lessScore;
        // }
    }
    /*
     * 按下亮牌按钮
     * */
    public onShowCards(): void {
        // this.report("onShowCards");
        WebSocketInstance.getHallSocket().send(CMDConfig.SEND_SHOWCARDS, {});
        this.visible = false;
    }
    /*
     * 按下结束时亮牌按钮
     * */
    public onGameEndShowCard(): void {
        // this.report("onGameEndShowCard");
    }
    /** 设置开始操作UI */
    public setOperateUI(): void {
        this.visible = true;
        console.log("setOperateUI", this.lTurnLessScore, this.lTurnMaxScore);
        if (this.lTurnLessScore == 0) {
            this.genzhuButton["icon"]["source"] = "rangpaiFont_png";
            this.genzhuButton["icon"]["visible"] = true;
            this.genzhuButton["icon2"]["visible"] = false;
            this.genzhuButton["bitLabel"]["visible"] = false;
        } else if (this.lTurnLessScore == this.lTurnMaxScore) {
            this.genzhuButton["icon"]["source"] = "allInFont_png";
            this.genzhuButton["icon"]["visible"] = true;
            this.genzhuButton["icon2"]["visible"] = false;
            this.genzhuButton["bitLabel"]["visible"] = false;
        } else {
            this.genzhuButton.labelDisplay.text = how.StringUtils.format(LanguageConfig.callNum, base.Utils.formatCurrency(this.lTurnLessScore));
            this.genzhuButton["icon"]["visible"] = false;
            this.genzhuButton["icon2"]["visible"] = true;
            this.genzhuButton["bitLabel"]["visible"] = true;
            this.genzhuButton["bitLabel"].text = base.Utils.formatCurrency(this.lTurnLessScore);
        }
        this.operateGroup.visible = true;
        this.autoOperateGroup.visible = false;
    }
    /*
     * 设置操作结束UI
     * */
    public setOperateOverUI(): void {
        if (AppData.getInstance().userData.operateAction != LanguageConfig.giveup) {
            this.setPreOperateUI()
        } else {
            this.quickOperatePanel.visible = false;
            this.operateGroup.visible = false;
            this.autoOperateGroup.visible = false;
        }
    }
    /** 设置预操作UI */
    public setPreOperateUI(): void {
        this.quickOperatePanel.visible = false;
        this.operateGroup.visible = false;
        this.buttonBar.visible = false;
        this.showCards.visible = false;
        this.gameEndShowCard.visible = false;
        this.autoOperateGroup.visible = true;
    }
    /*
     * 显示按钮条（自动坐下按钮）
     * */
    // public showButtonBar(): void {
    //     this.visible = true;
    //     this.quickOperatePanel.visible = false;
    //     this.operateGroup.visible = false;
    //     this.autoOperateGroup.visible = false;
    //     this.showCards.visible = false;
    //     this.gameEndShowCard.visible = false;
    //     this.buttonBar.visible = true;
    // }
    /*
     * 显示亮牌按钮
     * */
    public showShowCardsButton(): void {
        this.quickOperatePanel.visible = false;
        this.operateGroup.visible = false;
        this.buttonBar.visible = false;
        this.gameEndShowCard.visible = false;
        this.autoOperateGroup.visible = false;
        // this.showCards.visible = true;
        this.showCards.visible = false;
    }
    /** 显示结束时亮牌按钮 */
    public showGameEndShowCard(): void {
        this.quickOperatePanel.visible = false;
        this.operateGroup.visible = false;
        this.buttonBar.visible = false;
        this.showCards.visible = false;
        this.autoOperateGroup.visible = false;
        // this.gameEndShowCard.visible = true;
        this.gameEndShowCard.visible = false;
    }
    /*
     * 游戏开始隐藏亮牌和结束时亮牌按钮
     * */
    public gameStartHideButton(): void {
        this.showCards.visible = false;
        this.gameEndShowCard.visible = false;
        this.gameEndShowCard.selected = false;
    }
    //显示或隐藏弃牌按钮
    // public hideOrShowQipaiButton(visible: boolean): void {
    //     // this.qipaiButton.visible = visible;
    // }
    //更新自动让牌按钮label
    public updataZiDongRangPaiButtonLabel(text: string): void {
        this.zidongrangpaiButton.labelDisplay.text = text;
    }
    /** 更新自动跟注按钮状态 */
    public updateGenzhuLabel(): void {
        if(this.allin) return;
        var text = this.zidongrangpaiButton.labelDisplay.text;
        // var gameData = NewGameSceneData.getInstance();
        if (this.lTurnLessScore > 0) {//如果超过自己跟注额度
            if (this.lTurnLessScore < this.lTurnMaxScore) {//如果没有超过自己的最大下注金额
                this["zidongrangpaiButton"].labelDisplay.text = how.StringUtils.format(LanguageConfig.callNum, base.Utils.formatCurrency(this.lTurnLessScore));
                this["zidongrangpaiButton"]["icon"]["visible"] = false;
                this["zidongrangpaiButton"]["icon2"]["visible"] = true;
                this["zidongrangpaiButton"]["bitLabel"]["visible"] = true;
                this["zidongrangpaiButton"]["bitLabel"].text = base.Utils.formatCurrency(this.lTurnLessScore);
            } else {
                this["zidongrangpaiButton"].labelDisplay.text = LanguageConfig.genzhuButtonText2;
                this["zidongrangpaiButton"]["icon"]["source"] = "allInFont_png";
                this["zidongrangpaiButton"]["icon"]["visible"] = true;
                this["zidongrangpaiButton"]["icon2"]["visible"] = false;
                this["zidongrangpaiButton"]["bitLabel"]["visible"] = false;
            }
        } else if (this.lTurnLessScore == 0) {//如果小于自己下金额
            this.zidongrangpaiButton.labelDisplay.text = LanguageConfig.autoGenzhuLabel;
            this["zidongrangpaiButton"]["icon"]["source"] = "zidongrangpaiFont_png";
            this["zidongrangpaiButton"]["icon"]["visible"] = true;
            this["zidongrangpaiButton"]["icon2"]["visible"] = false;
            this["zidongrangpaiButton"]["bitLabel"]["visible"] = false;
        }
        if (this._preClick && this._preClick.name == "zidongrangpaiButton" && text != this["zidongrangpaiButton"].labelDisplay.text) {
            this._preClick.selected = false;
            this._preClick = null;
        }
    }

    /** 倒计时结束自动操作 */
    public autoLead(): void {
        if (this.tipDialog && this.tipDialog.parent) {
            how.Application.closeWindow(this.tipDialog);
            this.tipDialog = null;
        }
        // var gameSceneData: NewGameSceneData = NewGameSceneData.getInstance();
        if (this.lTurnLessScore == 0) {//如果最小加注是0则说明可以让牌
            // if (this.isSend) {
            //     this.isSend = false;
                this.setPreOperateUI();
                //停止自己出牌倒计时
                // this.communicate(NewPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
                // let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
                // pv.stopClock();
                // pv.showCards();
                var lessScore: number = this.lTurnLessScore ? this.lTurnLessScore : 0;
                WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CALL_CHIP, { lScore: lessScore });
                // NewGameSceneData.getInstance().myLAddScoreCount += lessScore;
            // }
        } else {
            // this.isSend = false;
            this.showGameEndShowCard();
            //停止自己出牌倒计时
            // this.communicate(NewPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
            // let pv:NewPlayerView = <NewPlayerView>D.App.gameView.playerList[0];
            // pv.stopClock();
            // pv.showCards();
            WebSocketInstance.getHallSocket().send(CMDConfig.SEND_QIPAi);
            // this.callUI("hideOrShowQipaiButton", false);
        }
        // this.jiazhuPanelVisible = false;
    }

/////////////////////////////快捷操作面板///////////////////////////////

    public ondaMang3(): void {
        this.jiazhu(Math.floor(this.dichiValue / 2));
    }
    public ondaMang5(): void {
        this.jiazhu(Math.floor(this.dichiValue / 3 * 2));
    }
    public ondaMang10(): void {
        this.jiazhu(Math.floor(this.dichiValue));
    }
    /** 按下AllIN按钮 */
    public onallInBtn(): void {
        var lessScore: number = this.lTurnMaxScore ? this.lTurnMaxScore : 0;
        this.jiazhu(lessScore);
    }
}
